package com.xkw.autocode.code;

import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.xkw.autocode.jdbc.entity.ColumnEntity;

/**
 * 生成java get&set的方法
 * 
 * @author xiangkaiwei
 *
 */
public class JavaTemplateUtils {

	private JavaTemplateUtils() {
	}

	private static final String T_KEY = "\t/**";

	private static final String T_KEY2 = "\t * ";
	
	private static final int LINE_LENGTH = 60;

	/**
	 * 获取java类属性的定义
	 * 
	 * @param columns
	 *            属性集合
	 * @param style
	 *            样式：1、doc形式的注释样式
	 *            样式：2、注释在右侧
	 */
	public static String getJavaProperty(List<ColumnEntity> columns, int style) {
		StringBuilder sbl = new StringBuilder();

		int index = 0;
		for (ColumnEntity column : columns) {

//			if (column.getColumnName().equalsIgnoreCase("id")) {
//				continue;
//			}

			// java 中数据类型
			String javaType = CodeUtils.getJavaType(column.getJdbcType());

			// 属性名
			String attrName = CodeUtils.getAttrName(column.getColumnName());

			// 注释
			String desc = column.getComments();

			//第一种注释格式
			if(style == 1) {
				sbl.append(getAttrDesc1(desc));
			}else if(style == 4) {
				sbl.append(getAttrDesc4(desc,javaType,column.getDefaultValue(),index ++));
			}else if(style == 5) {
				sbl.append(getAttrDesc5(desc,javaType,column.getDefaultValue(),index ++));
			}else if(style == 6) {
				sbl.append(getAttrDesc6(desc,javaType,column.getDefaultValue(),index ++));
			}else if(style == 7) {
				sbl.append(getAttrDesc7(desc,javaType,column.getDefaultValue(),index ++));
			}
			
			String line = "\tprivate " + javaType + " " + attrName + ";";
			
			sbl.append(line);
			
			//第二种注释格式
			if(style == 2) {
				sbl.append(getAttrDesc2(line,desc));
			}else if(style == 3) {
				sbl.append(getAttrDesc3(desc));
			}
			sbl.append("\n\n");
		}

		return sbl.toString();
	}

	/**
	 * 生成get&set方法
	 * 
	 * @param columns
	 *            属性集合
	 */
	public static String getJavaPropertyGetSet(List<ColumnEntity> columns) {

		StringBuilder sbl = new StringBuilder();

		for (ColumnEntity column : columns) {

			String columnName = column.getColumnName();

//			if (columnName.equalsIgnoreCase("id")) {
//				continue;
//			}

			// java 中数据类型
			String javaType = CodeUtils.getJavaType(column.getJdbcType());

			// 属性名
			String attrName = CodeUtils.getAttrName(columnName);

			// 首字母变大写的属性名
			String upAttrName = CodeUtils.getUpAttr(attrName);

			// 注释
			String desc = column.getComments();

			sbl.append(T_KEY).append("\n");
			sbl.append(T_KEY2).append("GET.").append(desc).append("\n");
			sbl.append("\t*/").append("\n");
			sbl.append("\tpublic " + javaType + " get" + upAttrName).append("(){\n");
			sbl.append("\t\treturn this.").append(attrName).append(";\n");
			sbl.append("\t}").append("\n").append("\n");

			sbl.append(T_KEY).append("\n");
			sbl.append(T_KEY2).append("SET.").append(desc).append("\n");
			sbl.append("\t*/").append("\n");
			sbl.append("\tpublic void set" + upAttrName).append("(" + javaType + " " + attrName + "){\n");
			sbl.append("\t\t this.").append(attrName).append("=").append(attrName).append(";\n");
			sbl.append("\t}").append("\n").append("\n");
		}

		return sbl.toString();
	}

	private static String getAttrDesc1(String desc) {
		if(StringUtils.isEmpty(desc)) {
			return "";
		}
		
		StringBuilder sbl = new StringBuilder();
		sbl.append(T_KEY).append("\n");
		sbl.append(T_KEY2).append(desc).append("\n");
		sbl.append("\t*/").append("\n");
		return sbl.toString();
	}
	
	private static String getAttrDesc2(String line,String desc) {
		if(StringUtils.isEmpty(desc)) {
			return "";
		}
		if(line.length() >= LINE_LENGTH) {
			return " //" + desc;
		}
		
		StringBuilder sbl = new StringBuilder();
		while(sbl.length() < LINE_LENGTH - line.length()) {
			sbl.append(" ");
		}
		sbl.append("//").append(desc);
		return sbl.toString();
	}
	
	private static String getAttrDesc3(String desc) {
		if(StringUtils.isEmpty(desc)) {
			return "";
		}
		
		StringBuilder sbl = new StringBuilder();
		sbl.append(" // ").append(desc);
		return sbl.toString();
	}
	
	private static String getAttrDesc4(String desc,String javaType,String defualtValue,int index) {
		if(StringUtils.isEmpty(desc)) {
			desc = "";
		}
		StringBuilder sbl = new StringBuilder();
		sbl.append("\t@ApiParam(value=\""+desc+"\"");
		
		if(javaType.equalsIgnoreCase("Integer") || javaType.equalsIgnoreCase("Long")) {
			sbl.append(",example=\"0\"");
		}
		if(StringUtils.isNotEmpty(defualtValue)) {
			sbl.append(",defaultValue=\""+ defualtValue +"\"");
		}
		
		sbl.append(")").append("\n");
		return sbl.toString();
	}
	
	private static String getAttrDesc5(String desc,String javaType,String defualtValue,int index) {
		if(StringUtils.isEmpty(desc)) {
			desc = "";
		}
		StringBuilder sbl = new StringBuilder();
		sbl.append("\t@ApiModelProperty(value=\""+desc+"\",position="+index);
		
		if(javaType.equalsIgnoreCase("Integer") || javaType.equalsIgnoreCase("Long")) {
			sbl.append(",example=\"0\"");
		}
		
		sbl.append(")").append("\n");
		return sbl.toString();
	}
	
	private static String getAttrDesc6(String desc,String javaType,String defualtValue,int index) {
		if(StringUtils.isEmpty(desc)) {
			desc = "";
		}
		StringBuilder sbl = new StringBuilder();
		sbl.append("\t@ApiModelProperty(value=\""+desc+"\"");
		
		if(StringUtils.isNotEmpty(defualtValue)) {
			sbl.append(",example=\""+ defualtValue +"\"");
		}
		
		sbl.append(")").append("\n");
		return sbl.toString();
	}
	
	private static String getAttrDesc7(String desc,String javaType,String defualtValue,int index) {
		if(StringUtils.isEmpty(desc)) {
			desc = "";
		}
		StringBuilder sbl = new StringBuilder();
		sbl.append("\t@ApiModelProperty(value=\""+desc+"\",position="+index);
		
		
		sbl.append(")").append("\n");
		return sbl.toString();
	}
}
